/*
 * Copyright 2008. Mount Sinai Hospital, Toronto, Canada.
 * 
 * Licensed under the Apache License, Version 2.0. You
 * can find a copy of the license at:
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * IN NO EVENT SHALL MOUNT SINAI HOSPITAL BE LIABLE TO ANY PARTY FOR DIRECT, 
 * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST 
 * PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, 
 * EVEN IF MOUNT SINAI HOSPITAL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 
 * DAMAGE.
 * 
 * MOUNT SINAI HOSPITAL SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF 
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE AND 
 * ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS". 
 * MOUNT SINAI HOSPITAL HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, 
 * UPDATES, ENHANCEMENTS, OR MODIFICATIONS. 
 */

package com.sinai.mshab.format;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;

/**
 * A simple panel which appears anywhere to notify that a field is required.
 * 
 * @author serge
 */
public class NotifyPanel extends FlexTable {

	/**
	 * The label on which the message is displayed.
	 */
	private Label message_label;

	/**
	 * The icon that appears beside the message.
	 */
	private Image icon;

	/**
	 * The image bundle that holds the default icons.
	 */
	private MshabImageBundle defaultIcons;

	/**
	 * Creates a default notification
	 */
	public NotifyPanel() {
		setUp("This field is required");
	}

	/**
	 * Creates a notification with a custom message
	 * 
	 * @param message
	 *            custom error message to show if validation fails
	 */
	public NotifyPanel(String message) {
		setUp(message);
	}

	/**
	 * Set the text of the notifier.
	 * 
	 * @param text
	 *            the text to display
	 */
	public void setText(String text) {
		message_label.setText(text);
	}

	/**
	 * This puts the notify panel together.
	 * 
	 * @param error_message
	 */
	private void setUp(String error_message) {

		// create our default icon ImageBundle
		defaultIcons = GWT.create(MshabImageBundle.class);

		setStyleName("mshab-validationNotify");
		message_label = new Label(error_message);
		setWidget(0, 1, message_label);
		getCellFormatter().setVerticalAlignment(0, 0,
				HasVerticalAlignment.ALIGN_MIDDLE);
		getCellFormatter().setVerticalAlignment(0, 1,
				HasVerticalAlignment.ALIGN_MIDDLE);
	}

	/**
	 * Set a specific icon on the (0,0) position.
	 * 
	 * @param path
	 *            the path to the image of the icon.
	 */
	public void setIcon(Image icon) {
		clearCell(0, 0);
		setWidget(0, 0, icon);
	}

	/**
	 * Hides the default icon.
	 */
	public void hideIcon() {
		icon.setVisible(false);
	}

	/**
	 * Hides the background and border of the notification
	 */
	public void hideBackground() {
		String cssName = this.getStyleName();
		if (cssName.indexOf("-background") != -1) {
			setStyleName(cssName.substring(0, cssName
					.lastIndexOf("-background")));
		}
	}

	/**
	 * Shows the background and border of the notification
	 */
	public void showBackground() {
		String cssName = this.getStyleName();
		if (cssName.indexOf("-background") == -1) {
			setStyleName(cssName + "-background");
		}
	}

	/**
	 * The notification assumes the state of an info box.
	 */
	public void infoMode() {
		setIcon(defaultIcons.getInfo().createImage());
		setStyleName("mshab-notify-info");
	}

	/**
	 * The notification assumes the state of an info box with a different icon.
	 * 
	 * @param icon
	 *            the icon to use instead of the default one.
	 */
	public void infoMode(Image icon) {
		setIcon(icon);
		setStyleName("mshab-notify-info");
	}

	/**
	 * The notification assumes the state of an warning box.
	 */
	public void warningMode() {
		setIcon(defaultIcons.getWarning().createImage());
		setStyleName("mshab-notify-warning");
	}

	/**
	 * The notification assumes the state of an warning box with a different
	 * icon.
	 * 
	 * @param icon
	 *            the icon to use instead of the default one.
	 */
	public void warningMode(Image icon) {
		setIcon(icon);
		setStyleName("mshab-notify-warning");
	}

	/**
	 * The notification assumes the state of an error box.
	 */
	public void errorMode() {
		setIcon(defaultIcons.getError().createImage());
		setStyleName("mshab-notify-error");
	}

	/**
	 * The notification assumes the state of an error box with a different icon.
	 * 
	 * @param icon
	 *            the icon to use instead of the default one.
	 */
	public void errorMode(Image icon) {
		setIcon(icon);
		setStyleName("mshab-notify-error");
	}
}
